import 'package:flutter/material.dart';
import 'package:flutter_09_game/base/widget/base_list_get_builder.dart';
import 'package:flutter_09_game/base/widget/base_list_more_get_builder.dart';
import 'package:flutter_09_game/config/app_color.dart';
import 'package:flutter_09_game/generated/assets.dart';
import 'package:flutter_09_game/generated/locales.g.dart';
import 'package:flutter_09_game/pages/deposit/deposit_record/deposit_record_team/deposit_record_team_logic.dart';
import 'package:flutter_09_game/pages/deposit/models/model_record_team.dart';
import 'package:flutter_09_game/pages/deposit/widgets/widget_record_funds.dart';

// import 'package:flutter_09_game/pages/deposit/widget/widget_record_funds.dart';
import 'package:flutter_09_game/user/app_user_manager.dart';
import 'package:flutter_09_game/utils/log/utils_log.dart';
import 'package:flutter_09_game/utils/utils_other.dart';
import 'package:flutter_09_game/widgets/btn_dropdown.dart';
import 'package:flutter_09_game/widgets/button/debounce_gesture_detector.dart';
import 'package:flutter_09_game/widgets/crypto_tag_selector.dart';
import 'package:flutter_09_game/widgets/empty/empty_no_data.dart';
import 'package:flutter_09_game/widgets/image/auto_image.dart';
import 'package:flutter_09_game/widgets/image/image_view.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import 'deposit_record_team_list_logic.dart';

class DepositRecordTeamListPage extends StatelessWidget {
  DepositRecordTeamListPage({
    super.key,
    required this.type,
  });

  late final DepositRecordTeamListLogic logic = Get.put(
      DepositRecordTeamListLogic(
        type: type,
      ),
      tag: type);
  final String type;

  @override
  Widget build(BuildContext context) {
    return BaseListMoreGetBuilder<DepositRecordTeamListLogic>(
      init: logic,
      tag: type,
      emptyBuilder: () {
        return const SizedBox(
          height: 16,
        );
      },
      bodyTopViewBuilder: (_) {
        return _buildNotTeamTop();
      },
      headerBuilder: (_) {
        return _buildNotTeamHeader();
      },
      builder: (_) {
        clog('dalksdjlksa    $type');
        return _buildNotTeamList();
      },
    );
  }

  Widget _buildTopDropdown(List<String> items,
      {required String value,
      required Function(String) onChanged,
      MainAxisAlignment? mainAxisAlignment,
      double? width,
      Color? btnColor}) {
    return BtnDropdown(
        items: items,
        value: value,
        mainAxisAlignment: mainAxisAlignment,
        onChanged: (value) {
          onChanged.call(value);
        },
        width: width,
        child: Container(
          padding: EdgeInsets.symmetric(horizontal: 10.w, vertical: 12.w),
          decoration: BoxDecoration(
              border: Border(
                  bottom: BorderSide(
                      color: AppColor.textPrimary.withOpacity(0.15), width: 1)),
              color: Colors.black.withOpacity(0.3),
              borderRadius: BorderRadius.circular(8.w)),
          child: Row(
            children: [
              Expanded(
                  child: Text(
                value,
                style: TextStyle(
                  fontSize: 14.sp,
                  fontWeight: FontWeight.w900,
                  color: btnColor ?? AppColor.textPrimary.withOpacity(0.94),
                ),
              )),
              AutoImage(
                Assets.promotionCommissionCaretDown,
                width: 16.w,
              )
            ],
          ),
        ));
  }

  Widget _buildNotTeamTop() {
    Widget inputWidget = Container(
      padding: EdgeInsets.symmetric(horizontal: 6.w, vertical: 7.w)
          .copyWith(left: 12.w),
      decoration: BoxDecoration(
          border: Border(
              bottom: BorderSide(
                  color: AppColor.textPrimary.withOpacity(0.15), width: 1)),
          color: Colors.black.withOpacity(0.3),
          borderRadius: BorderRadius.circular(8.w)),
      child: Row(
        children: [
          Expanded(
              child: TextField(
            controller: logic.idController,
            maxLength: 20,
            style: TextStyle(
              fontSize: 14.sp,
              fontWeight: FontWeight.w500,
              color: AppColor.textPrimary.withOpacity(0.94),
            ),
            decoration: InputDecoration(
                border: InputBorder.none,
                enabledBorder: InputBorder.none,
                focusedBorder: InputBorder.none,
                disabledBorder: InputBorder.none,
                errorBorder: InputBorder.none,
                focusedErrorBorder: InputBorder.none,
                counterText: '',
                hintText: LocaleKeys.enterMemberId.tr,
                isDense: true,
                // 紧凑布局，有助于垂直对齐
                hintStyle: TextStyle(
                  fontSize: 14.sp,
                  fontWeight: FontWeight.w400,
                  color: AppColor.textPrimary.withOpacity(0.5),
                ),
                contentPadding: EdgeInsets.zero,
                // 调整内边距
                isCollapsed: true),
            onSubmitted: (text) {
              logic.refreshController.requestRefresh();
            },
          )),
          GestureDetector(
            onTap: () {
              logic.refreshController.requestRefresh();
            },
            child: AutoImage(
              Assets.commonSearchBtn,
              width: 32.w,
            ),
          )
        ],
      ),
    );

    Widget symbolWidget = Obx(() => _buildTopDropdown(
            [...AppUserManager().walletList.map((f) => f.symbol ?? '')],
            value: AppUserManager().currentWalletRx.value.symbol ?? '',
            mainAxisAlignment: MainAxisAlignment.start, onChanged: (value) {
          AppUserManager().selectUserWallet(value);
          logic.refreshController.requestRefresh();
        }));

    return Column(
      children: [
        Container(
          padding: EdgeInsets.all(16.w).copyWith(bottom: 24.w),
          decoration: BoxDecoration(
            color: AppColor.navBgColor,
            borderRadius: BorderRadius.only(
              bottomLeft: Radius.circular(30.w),
              bottomRight: Radius.circular(30.w),
            ),
          ),
          child: Obx(() {
            clog('message   $type');
            return Column(
              children: [
                Visibility(
                    visible: logic.getTag() == LocaleKeys.game.tr,
                    child: _buildTopDropdown(logic.gameItems,
                        value: logic.gameItems[logic.gameType.value],
                        width: 1.sw - 32.w, onChanged: (value) {
                      logic.gameType.value = logic.gameItems.indexOf(value);
                      // logic.updateTimeSelection(value);
                          logic.refreshController.requestRefresh();
                    }).marginOnly(bottom: 4.w)),
                // 4.verticalSpaceFromWidth,

                Row(
                  children: [
                    Expanded(
                        child: _buildTopDropdown([
                      LocaleKeys.today.tr,
                      LocaleKeys.yesterday.tr,
                      LocaleKeys.threeDays.tr,
                      LocaleKeys.sevenDays.tr,
                      LocaleKeys.custom.tr,
                    ], btnColor: AppColor.primary, value: logic.timeRx.value,
                            onChanged: (value) {
                      logic.updateTimeSelection(value);
                    })),
                    4.horizontalSpace,
                    Expanded(
                        child: logic.getTag() == LocaleKeys.game.tr
                            ? inputWidget
                            : symbolWidget),
                  ],
                ),
                4.verticalSpaceFromWidth,
                if (logic.getTag() != LocaleKeys.game.tr) inputWidget
              ],
            );
          }),
        ),
      ],
    );
  }

  Widget _buildNotTeamHeader() {
    return Padding(
      padding: EdgeInsets.symmetric(horizontal: 16.w),
      child: Column(
        children: [
          16.verticalSpaceFromWidth,
          Obx(() {
            if (logic.recordTeamRx.value.mine != null) {
              return WidgetRecordFunds(
                data: logic.recordTeamRx.value.mine!,
                isHash: logic.getIsHash(),
                isTeamMode: false,
                onToggleExpand: () {
                  logic.onToggleExpand(logic.recordTeamRx.value.mine!);
                },
              );
            }
            return const SizedBox.shrink();
          }),
          16.verticalSpaceFromWidth,
          // 团队整体汇总结果
          Obx(() {
            if (logic.recordTeamRx.value.team != null) {
              return WidgetRecordFunds(
                isHash: logic.getIsHash(),
                data: logic.recordTeamRx.value.team!,
                // title: '团队整体汇总结果',
                isTeamMode: true,
                onToggleExpand: () {
                  logic.onToggleExpand(logic.recordTeamRx.value.team!);
                },
              );
            }
            return const SizedBox.shrink();
          }),
          // 16.verticalSpaceFromWidth,
        ],
      ),
    );
  }

  Widget _buildNotTeamList() {
    return SliverPadding(
      padding: EdgeInsets.symmetric(horizontal: 16.w).copyWith(bottom: 16.w),
      sliver: SliverList.builder(
          itemCount: logic.list.length,
          itemBuilder: (c, index) {
            ModelRecordTeamItem item = logic.list[index];
            return WidgetRecordFunds(
              data: logic.list[index],
              isHash: logic.getIsHash(),
              onToggleExpand: () {
                logic.onToggleExpand(item);
              },
              userHeader: Row(
                children: [
                  // 头像
                  Container(
                      width: 44.w,
                      height: 44.w,
                      decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(22.w),
                        border: Border.all(
                          color: AppColor.color22242A,
                          width: 1.w,
                        ),
                      ),
                      child: Stack(
                        children: [
                          Positioned.fill(
                              child: ImageView(
                            imageUrl: item.userAvatar,
                            radius: 22.w,
                            fit: BoxFit.cover,
                          )),
                          Positioned(
                            bottom: 0,
                            right: 0,
                            child: Container(
                              width: 16.w,
                              height: 16.w,
                              decoration: BoxDecoration(
                                color: AppColor.homePrizePoolBgIndicator
                                    .withOpacity(0.12),
                                shape: BoxShape.circle,
                              ),
                              child: Center(
                                child: Container(
                                  width: 8.w,
                                  height: 8.w,
                                  decoration: const BoxDecoration(
                                    color: AppColor.homePrizePoolIndicator,
                                    shape: BoxShape.circle,
                                  ),
                                ),
                              ),
                            ),
                          )
                        ],
                      )),
                  12.horizontalSpace,
                  // 用户信息
                  Expanded(
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        // 用户名和直属人数
                        Row(
                          children: [
                            Flexible(
                              child: Text(
                                item.userName ?? '--',
                                style: TextStyle(
                                  fontFamily: 'PingFang SC',
                                  fontSize: 12.sp,
                                  fontWeight: FontWeight.w500,
                                  color: AppColor.textPrimary.withOpacity(0.94),
                                ),
                              ),
                            ),
                            8.horizontalSpace,
                            // 直属人数

                            AutoImage(
                              Assets.promotionCommissionUser,
                              width: 16.w,
                              height: 16.w,
                            ),
                            // 4.horizontalSpace,
                            Text(
                              '${(item.teamMemberCount ?? 0) + 1}',
                              style: TextStyle(
                                fontFamily: 'PingFang SC',
                                fontSize: 14.sp,
                                fontWeight: FontWeight.w400,
                                color: AppColor.textPrimary.withOpacity(0.7),
                              ),
                            ),
                          ],
                        ),
                        // 4.verticalSpaceFromWidth,
                        // 会员ID
                        DebounceGestureDetector(
                            onTap: () => UtilsOther.copyMemberId(
                                item.userIdentity ?? ''),
                            child: Row(
                              children: [
                                Flexible(
                                  child: Text(
                                    '${LocaleKeys.memberId.tr}：${item.userIdentity ?? '--'}',
                                    style: TextStyle(
                                      fontFamily: 'PingFang SC',
                                      fontSize: 12.sp,
                                      fontWeight: FontWeight.w400,
                                      color:
                                          AppColor.textPrimary.withOpacity(0.7),
                                    ),
                                  ),
                                ),
                                4.horizontalSpace,
                                AutoImage(
                                  Assets.promotionCommissionCopy,
                                  width: 16.w,
                                  height: 16.w,
                                ),
                              ],
                            )),
                        4.verticalSpaceFromWidth,
                        // 投注地址
                        Text(
                          '${LocaleKeys.betAddress.tr}：${UtilsOther.formatWithdrawAddress(item.withdrawAddress)}',
                          style: TextStyle(
                            fontFamily: 'PingFang SC',
                            fontSize: 12.sp,
                            fontWeight: FontWeight.w400,
                            color: AppColor.textPrimary.withOpacity(0.7),
                          ),
                        ),
                      ],
                    ),
                  ),
                ],
              ),
            ).marginOnly(top: 16.w);
          }),
    );
  }

// Widget _buildTeamTop() {
//   return Column(
//     children: [
//       Container(
//         padding: EdgeInsets.all(16.w).copyWith(bottom: 24.w),
//         decoration: BoxDecoration(
//           color: AppColor.navBgColor,
//           borderRadius: BorderRadius.only(
//             bottomLeft: Radius.circular(30.w),
//             bottomRight: Radius.circular(30.w),
//           ),
//         ),
//         child: Obx(() {
//           return Column(
//             children: [
//               Row(
//                 children: [
//                   Expanded(
//                       child: _buildTopDropdown([
//                     LocaleKeys.today.tr,
//                     LocaleKeys.yesterday.tr,
//                     LocaleKeys.threeDays.tr,
//                     LocaleKeys.sevenDays.tr,
//                     LocaleKeys.custom.tr,
//                   ], btnColor: AppColor.primary, value: logic.timeRx.value,
//                           onChanged: (value) {
//                     logic.updateTimeSelection(value);
//                   })),
//                   4.horizontalSpace,
//                   Expanded(
//                       child: _buildTopDropdown([
//                     LocaleKeys.allCoins.tr,
//                     ...AppUserManager().walletList.map((f) => f.symbol ?? '')
//                   ], value: '', onChanged: (value) {
//                     // logic.symbolRx.value = value;
//                   })),
//
//                 ],
//               ),
//               4.verticalSpaceFromWidth,
//               Container(
//                 padding: EdgeInsets.symmetric(horizontal: 6.w, vertical: 10.w)
//                     .copyWith(left: 12.w),
//                 decoration: BoxDecoration(
//                     border: Border(
//                         bottom: BorderSide(
//                             color: AppColor.textPrimary.withOpacity(0.15),
//                             width: 1)),
//                     color: Colors.black.withOpacity(0.3),
//                     borderRadius: BorderRadius.circular(8.w)),
//                 child: Row(
//                   children: [
//                     Expanded(
//                         child: TextField(
//                       controller: logic.idController,
//                       maxLength: 20,
//                       style: TextStyle(
//                         fontSize: 14.sp,
//                         fontWeight: FontWeight.w500,
//                         color: AppColor.textPrimary.withOpacity(0.94),
//                       ),
//                       decoration: InputDecoration(
//                         border: InputBorder.none,
//                         enabledBorder: InputBorder.none,
//                         focusedBorder: InputBorder.none,
//                         disabledBorder: InputBorder.none,
//                         errorBorder: InputBorder.none,
//                         focusedErrorBorder: InputBorder.none,
//                         counterText: '',
//                         hintText: LocaleKeys.enterMemberId.tr,
//                         isDense: true,
//                         // 紧凑布局，有助于垂直对齐
//                         hintStyle: TextStyle(
//                           fontSize: 14.sp,
//                           fontWeight: FontWeight.w400,
//                           color: AppColor.textPrimary.withOpacity(0.5),
//                         ),
//                         contentPadding: EdgeInsets.zero, // 调整内边距
//                       ),
//                       onSubmitted: (text) {
//                         logic.refreshController.requestRefresh();
//                       },
//                     )),
//                     GestureDetector(
//                       onTap: () {
//                         logic.refreshController.requestRefresh();
//                       },
//                       child: AutoImage(
//                         Assets.commonSearchBtn,
//                         width: 32.w,
//                       ),
//                     )
//                   ],
//                 ),
//               ),
//             ],
//           );
//         }),
//       ),
//     ],
//   );
// }
}
